bonepfandomcom-20200214-history
TGA Image format
Todo *Implementaton of compressed image data *Implementation of other image type (only handled type=2 at this moment) Reference *http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf *http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/ *http://www.fileformat.info/format/tga/egff.htm Implementation This is a VB .net implementation. This code will be improved. In this moment, the image data has NO compression. In the Form1 form, I have added the following controls: *btnEncode (Button) *txtOutput (Textbox) *pnlOutput (Panel) Imports System.IO Public Class Form1 Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click Dim input As New FileStream("tga3.tga", FileMode.Open) Dim bytes(CInt(input.Length - 1)) As Byte input.Read(bytes, 0, CInt(input.Length)) tgaEncode(bytes) input.Close() End Sub Private Sub tgaEncode(ByVal in_byte As Byte()) Dim outText As String Dim i As Integer outText = "" For i = 0 To in_byte.Length - 1 outText += in_byte(i).ToString + " " Next outText += vbCrLf + vbCrLf outText += "Size of Image ID field (byte 0): " + in_byte(0).ToString + vbCrLf outText += vbCrLf outText += "Color map type (byte 1): " + in_byte(1).ToString + " (0=No color map, 1=Color Map included)" + vbCrLf outText += vbCrLf outText += "Image type (byte 2): " + in_byte(2).ToString + vbCrLf outText += "- 0 = No image data included in file" + vbCrLf outText += "- 1 = Colormapped image data" + vbCrLf outText += "- 2 = Truecolor image data" + vbCrLf outText += "- 3 = Monochrome image data" + vbCrLf outText += "- 9 = Colormapped image data" + vbCrLf outText += "- 10 =Truecolor image data" + vbCrLf outText += "- 11 = Monochrome image data" + vbCrLf outText += vbCrLf outText += "If color map type is not 0, the Color Map Specification shows as follow." + vbCrLf outText += "Index of first color map entry (byte 3-4) (low-high): " + in_byte(3).ToString + " " + in_byte(4).ToString + vbCrLf outText += "Color map Length (byte 5-6) (low-high): " + in_byte(5).ToString + " " + in_byte(6).ToString + vbCrLf outText += "Color map Entry Size (byte 7): " + in_byte(7).ToString + vbCrLf outText += vbCrLf outText += "X coordinate of the lower left corner (byte 8-9): " + in_byte(8).ToString + " " + in_byte(9).ToString + vbCrLf outText += "Y coordinate of the lower left corner (byte 10-11): " + in_byte(10).ToString + " " + in_byte(11).ToString + vbCrLf outText += "Width of the image in pixels (byte 12-13): " + in_byte(12).ToString + " " + in_byte(13).ToString + vbCrLf outText += "Height of the image in pixels (byte 14-15): " + in_byte(14).ToString + " " + in_byte(15).ToString + vbCrLf outText += "Pixel Depth (number of bits per pixel) (byte 16): " + in_byte(16).ToString + vbCrLf outText += "Image Descriptor: (byte 17): " + in_byte(17).ToString + vbCrLf outText += "- Bits 0-3: number of attribute bits per pixel: " + ((in_byte(17) >> 3) And 1).ToString + ((in_byte(17) >> 2) And 1).ToString + ((in_byte(17) >> 1) And 1).ToString + ((in_byte(17) >> 0) And 1).ToString + vbCrLf outText += "- Bits 4: screen origin position (0=left, 1=right): " + ((in_byte(17) >> 4) And 1).ToString + vbCrLf outText += "- Bits 5: screen origin position (0=bottom, 1=top): " + ((in_byte(17) >> 5) And 1).ToString + vbCrLf outText += "- Bits 6-7: No used (or Data storage interleaving flag?): " + ((in_byte(17) >> 7) And 1).ToString + ((in_byte(17) >> 6) And 1).ToString + vbCrLf outText += vbCrLf outText += "Assume no IMAGE/COLOR MAP DATA AREA" + vbCrLf outText += vbCrLf outText += "Data Area: (Blue, Green, Red)" + vbCrLf Dim imageWidth As Integer imageWidth = in_byte(13) * 255 + in_byte(12) Dim imageHeight As Integer imageHeight = in_byte(15) * 255 + in_byte(14) Dim row As Integer Dim col As Integer Dim dataIndex As Integer Dim drawGraphic As System.Drawing.Graphics drawGraphic = Me.pnlOutput.CreateGraphics() dataIndex = 18 For row = 0 To imageHeight - 1 For col = 0 To imageWidth - 1 Dim pixelStr As String pixelStr = "(" + in_byte(dataIndex).ToString + " " + in_byte(dataIndex + 1).ToString + " " + in_byte(dataIndex + 2).ToString + ") " Dim colorPen As New Pen(Color.FromArgb(in_byte(dataIndex + 2), in_byte(dataIndex + 1), in_byte(dataIndex))) drawGraphic.DrawRectangle(colorPen, col, row, 1, 1) dataIndex += 3 outText += pixelStr Next outText += vbCrLf Next Me.txtOutput.Text = outText End Sub End Class Category:Data Compression Category:tga Category:vb .net